﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlServerCe;
using System.Data;

namespace KokosSearcher
{
	public class AuctionsDB
	{

		private bool connect()
		{
			try
			{
				SqlCeConnection connection = new SqlCeConnection(this.connectionString());

				if (connection.State == ConnectionState.Closed)
				{
					connection.Open();
				}
			}
			catch (Exception ex)
			{
				throw ex;
			}

			return true;
		}

		private int executeQuery(string p_query)
		{
			int rows = 0;

			try
			{
				SqlCeConnection connection = new SqlCeConnection(this.connectionString());

				if (connection.State == ConnectionState.Closed)
				{
					connection.Open();
				}

				// execute query
				SqlCeCommand query = new SqlCeCommand(p_query, connection);

				try
				{
					rows = query.ExecuteNonQuery();
				}
				catch (Exception ex)
				{
					throw ex;
				}
				finally
				{
					connection.Close();
				}

				connection = null;
			}
			catch (Exception ex)
			{
				throw ex;
			}

			return rows;
		}

		public bool addAuction(KokosAuction p_Auction)
		{
			// TODO: dodanie/aktualizacja rekordu w bazie
			string query = "";

			if (this.connect())
			{
				// sprawdzenie czy rekord jest już w bazie
				//query = "select count(*) from Auctions"
			}
			else
			{
				return false;
			}

			return true;
		}

		public bool deleteAuction()
		{
			return true;
		}

		public bool updateAuctionData()
		{
			return true;
		}

		public string connectionString()
		{
			string connectionString;
			string fileName = "AuctionDB.sdf";
			string password = "123456789";

			connectionString = string.Format("Data Source=\"{0}\";Password='{1}'", fileName, password);

			return connectionString;
		}
	}
}
